import { createStore } from 'vuex'
import { getuser } from '~/api/manager';
import { useCookies } from '@vueuse/integrations/useCookies';

// 创建一个新的 store 实例
const cookies = useCookies();
const store = createStore({
    state() {
        return {

            //用户信息
            user: {}
        }
    },
    mutations: {
        //记录用户信息
        SET_USERINFO(state, user) {
            state.user = user;
        }
    },
    actions: {
        //获取当前登录用户信息
        getinfo({ commit }) {
            return new Promise((resolve, reject) => {
                getuser(cookies.get("use_id")).then(res => {
                    commit("SET_USERINFO", res);
                    resolve(res)
                }).catch(err => reject(err))
            })
        },
        logout({commit}) {
            //移除cookie中的token
            cookies.remove("user-token");
            cookies.remove("use_id");
            //清除用户状态vuex
            commit("SET_USERINFO", {});
        }

    }
})

export default store